package org.holynite.fieldmanage.utils

import groovy.text.SimpleTemplateEngine

import javax.faces.context.FacesContext
import javax.inject.Named

/**
 * Created by midnite on 15/1/10.
 */
@Named("LocaleString")
class LocaleString {

	private ResourceBundle bundle
	private SimpleTemplateEngine engine

	LocaleString() {
		engine = new SimpleTemplateEngine()
		def localeLabelBundle = FacesContext.currentInstance.externalContext.getInitParameter("LocaleLabels")
		bundle = FacesContext.currentInstance.application.getResourceBundle(FacesContext.currentInstance, localeLabelBundle)
	}

	String fetch(String id) {
		try {
			if (!bundle.containsKey(id)) return ''
			return bundle.getString(id)
		} catch (NullPointerException e) {
			return ''
		}
	}

	String fetch(String id, def binding) {
		try {
			if (!bundle.containsKey(id)) return ''
			def value = engine.createTemplate(bundle.getString(id)).make(binding)
			return value
		} catch (NullPointerException e) {
			return ''
		}
	}
}
